import { message } from "antd"
import {
  getRechargeConfig,
  updateRechargeConfig,
  getWelfareConfig,
  updateWelfareConfig,
  getGiftConfig,
  updateGiftConfig,
  getInviteRewardsConfig,
  updateInviteRewardsConfig,

}  from '../services/rateSetting';


export default {
  namespace: 'activities',

  state: {
    chargeItem:{},
    welfareItem:{},
    giftItem:{},
    rewardItem:{},
  },

  effects: {
    *getRechargeConfig({payload}, { call, put }) {
      const data = yield call(getRechargeConfig);
      if(data.status == 0){
        yield put({
          type: 'updateState',
          payload: {
            chargeItem:data.result
          }
        })
      }else{
       message.error(`${data.message}`)
      }
    },

    *updateRechargeConfig({payload}, { call, put }) {
      const data = yield call(updateRechargeConfig,payload);
      if(data.status == 0){
        message.success("编辑成功!")
         yield put({type:"getRechargeConfig"})
      }else{
        message.error(`${data.message}`)
      }
    },

    *getWelfareConfig({payload}, { call, put }) {
      const data = yield call(getWelfareConfig);
      if(data.status == 0){
        yield put({
          type: 'updateState',
          payload: {
            welfareItem:data.result
          }
        })
      }else{
       message.error(`${data.message}`)
      }
    },

    *updateWelfareConfig({payload}, { call, put }) {
      const data = yield call(updateWelfareConfig,payload);
      if(data.status == 0){
        message.success("编辑成功!")
         yield put({type:"getWelfareConfig"})
      }else{
        message.error(`${data.message}`)
      }
    },

    *getGiftConfig({payload}, { call, put }) {
      const data = yield call(getGiftConfig);
      if(data.status == 0){
        yield put({
          type: 'updateState',
          payload: {
            giftItem:data.result
          }
        })
      }else{
       message.error(`${data.message}`)
      }
    },

    *updateGiftConfig({payload}, { call, put }) {
      const data = yield call(updateGiftConfig,payload);
      if(data.status == 0){
        message.success("编辑成功!")
         yield put({type:"getGiftConfig"})
      }else{
        message.error(`${data.message}`)
      }
    },

    *getInviteRewardsConfig({payload}, { call, put }) {
      const data = yield call(getInviteRewardsConfig);
      if(data.status == 0){
        yield put({
          type: 'updateState',
          payload: {
            rewardItem:data.result
          }
        })
      }else{
       message.error(`${data.message}`)
      }
    },

    *updateInviteRewardsConfig({payload}, { call, put }) {
      const data = yield call(updateInviteRewardsConfig,payload);
      if(data.status == 0){
        message.success("编辑成功!")
         yield put({type:"getInviteRewardsConfig"})
      }else{
        message.error(`${data.message}`)
      }
    },



  },

  reducers: {
    updateState (state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === "/activity/list") {
          dispatch({ type: "getRechargeConfig" });
          dispatch({ type: "getWelfareConfig"});
          dispatch({ type: "getGiftConfig"});
          dispatch({ type: "getInviteRewardsConfig"})
        }
      });
    }
  },

};
